package com.ymk.conifg;


import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.job.JobBO;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IJobService;
import com.ymk.service.QuartzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *@Author: zjb
 *@Date: 2023-05-21 15:25
 */
@Service
@Slf4j
public class ReportSubscribeServiceImpl implements CommandLineRunner {

	@Override
    public void run(String... args) throws Exception {
        init();
    }
    
     private void init() {
        // 初始化所有的已经启用的订阅
         ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                 .addParam("status", SysConstants.YES);
         List<JobBO> jobBOList = this.jobService.list(paramsBuilder.build());
        log.info("需要初始化的任务个数:{}", jobBOList.size());
        for (JobBO jobBO : jobBOList) {
            try {
                log.info("开始初始化订阅任务,任务name:{}", jobBO.getJobName());
                this.quartzService.addJob(jobBO.getJobCode(),jobBO.getJobGroup(),jobBO.getJobCode(),jobBO.getJobGroup(),jobBO.getCronExpression());
                if (SysConstants.YES.equals(jobBO.getStatus())){
                    this.quartzService.resumeJob(jobBO.getJobCode(),jobBO.getJobGroup());
                }else{
                    this.quartzService.pauseJob(jobBO.getJobCode(),jobBO.getJobGroup());
                }
            } catch (Exception e) {
                log.error("启动时初始化订阅任务失败:{}", e.getMessage());
            }
        }
    }

    @Autowired
    private IJobService jobService;
	@Autowired
    private QuartzService quartzService;
}
